package cn.schoolwow.quickdao.module.database.dml.existQuery.flow.instances;

import cn.schoolwow.quickdao.domain.QuickDAOConfig;
import cn.schoolwow.quickdao.domain.entity.Entity;
import cn.schoolwow.quickdao.domain.entity.Property;
import cn.schoolwow.quickdao.module.database.parent.domain.ResultSetConsumer;
import cn.schoolwow.quickdao.module.database.parent.flow.executor.ExecuteQueryConnectionFlow;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.query.instance.QueryInstance;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class GetInstanceArrayCountBySingleKeyFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        setStatement(flowContext);
        setParameters(flowContext);
        executeStatement(flowContext);
    }

    @Override
    public String name() {
        return "根据单个字段查询记录数组个数";
    }

    private void setStatement(FlowContext flowContext) {
        QuickDAOConfig quickDAOConfig = flowContext.checkInstanceData(QuickDAOConfig.class);
        Entity entity = flowContext.checkData("entity", Entity.class);
        Object[] instances = flowContext.checkData("instances", Object[].class);
        Property singleProperty = flowContext.checkData("singleProperty", Property.class);
        
        StringBuilder sqlBuilder = new StringBuilder();
        sqlBuilder.append("select count(1) from " + quickDAOConfig.databaseContext.databaseProvider.escape(entity.tableName) + " where ");
        sqlBuilder.append(quickDAOConfig.databaseContext.databaseProvider.escape(singleProperty.column) + " in (");
        for(int i=0;i<instances.length;i++){
            sqlBuilder.append("?,");
        }
        sqlBuilder.deleteCharAt(sqlBuilder.length()-1);
        sqlBuilder.append(")");
        flowContext.putTemporaryData("sql", sqlBuilder.toString());
    }

    private void setParameters(FlowContext flowContext) {
        Property singleProperty = (Property) flowContext.checkData("singleProperty");

        List<Object> parameters = new ArrayList<>();
        Object[] instances = (Object[]) flowContext.checkData("instances");
        for(int i=0;i<instances.length;i++){
            Object value = QueryInstance.newQuery(instances[i])
                    .execute()
                    .getSingleFieldValue(singleProperty.name);
            parameters.add(value);
        }
        flowContext.putTemporaryData("parameters", parameters);
    }

    private void executeStatement(FlowContext flowContext){
        flowContext.startFlow(new ExecuteQueryConnectionFlow())
                .putTemporaryData("name", "根据单个字段查询记录个数")
                .putReturnData("resultSetConsumer",new ResultSetConsumer() {
                    @Override
                    public void consumeResultSet(ResultSet resultSet) throws Exception {
                        if(resultSet.next()){
                            flowContext.putData("count", resultSet.getLong(1));
                        }
                    }
                })
                .execute();
    }

}
